package com.ruoyi.project.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.project.domain.ProjectRisk;
import com.ruoyi.project.service.IProjectRiskService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.domain.Project;
import com.ruoyi.project.service.IProjectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目Controller
 * 
 * @author Q
 * @date 2024-05-28
 */
@RestController
@RequestMapping("/project/project")
public class ProjectController extends BaseController
{
    @Autowired
    private IProjectService projectService;
    @Autowired
    private IProjectRiskService projectRiskService;




    /**
     * 根据当前登录用户id查询项目
     */
    @GetMapping("/ProjectListByUserId")
    public AjaxResult list(){
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Long userId = loginUser.getUserId();
        System.out.println("userId = " + userId);
        List<Project> usersProjectIds = projectService.selectProjectByUserId(userId);
        return AjaxResult.success(usersProjectIds);
    }

    /**
     * 查询项目列表 只查询当前登录用户id的项目
     */
    @PreAuthorize("@ss.hasPermi('project:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(Project project)
    {

        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Long userId = loginUser.getUserId();
        // 查询项目列表 按照登录用户id
        startPage();
        List<Project> list = projectService.selectProjectListForUser(project,userId);
        return getDataTable(list);
    }

    /**
     * 获取项目详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:project:query')")
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") Long projectId)
    {


        return success(projectService.selectProjectByProjectId(projectId));
    }

    /**
     * 新增项目
     */
    @PreAuthorize("@ss.hasPermi('project:project:add')")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Project project)
    {
        //唯一性验证
        if(projectService.checkNameUnique(project)){
            return error("项目名称已存在,请换一个!");
        }

        return projectService.insertProject(project);
    }

    /**
     * 修改项目
     */
    @PreAuthorize("@ss.hasPermi('project:project:edit')")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Project project)
    {
        //唯一性验证
        if(projectService.checkNameUnique(project)){
            return error("项目名称已存在,请换一个!");
        }

        return toAjax(projectService.updateProject(project));
    }
    /**
     *  单删
     */
    @PreAuthorize("@ss.hasPermi('project:project:remove')")
    @Log(title = "项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{projectId}")
    public AjaxResult remove(@PathVariable Long projectId) {
        String msg = projectService.checkHasData(projectId);
        if(msg != null){
            return error("无法删除,此项目被["+msg+"]引用");
        }

        return projectService.deleteProjectByProjectId(projectId);
    }
    /**
     *  查风险表数据
     */
    @GetMapping(value = "/getRisk/{projectId}")
    public AjaxResult getRisk(@PathVariable("projectId") Long projectId)
    {
        List<ProjectRisk> projectRiskList = projectRiskService.getRiskCount(projectId);
        return success(projectRiskList);
    }
  /**
   *   项目和活动级联 材料表用的
   */
    @GetMapping("/cascaderProjectForActList")
    public AjaxResult cascaderProjectForActList(){
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Long userId = loginUser.getUserId();
        Integer[] projectIds = projectService.selectProjectIds(userId);

        List<Map<String, Object>> list = projectService.selectCascaderList(projectIds);
        return success(list);
    }
  /**
   *   项目和承包商级联 承包商表用的
   */
    @GetMapping("/cascaderProjectForConList")
    public AjaxResult cascaderProjectForConList(){
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Long userId = loginUser.getUserId();
        Integer[] projectIds = projectService.selectProjectIds(userId);

        List<Map<String, Object>> list = projectService.selectConCascaderList(projectIds);
        return success(list);
    }


}
